﻿using System;
using Besture.Common.Helper;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using Besture.Common;
using Besture.Bll;

namespace Besture.UI
{
    public partial class HotelsList : BasePage
    {
        #region 页面加载

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            //删除酒店ID为0的酒店地图信息
            TrafficDac trafficDac = new TrafficDac();
            trafficDac.DeleteByHotelsId(0);
            //初始化页面
            InitPage();
            BindData();
            AspxGridViewHelper.InitAspxGridView(grid);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        public void InitPage()
        {

        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            var dac = new HotelsDac();
            grid.DataSource = dac.GetDataTable();
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridCustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }
        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridRowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            var id = Convert.ToInt32(e.Keys[0]);
            var dac = new HotelsDac();
            dac.Delete(id);
            BindData();
        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbDel_Click(object sender, EventArgs e)
        {
            var ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            var dac = new HotelsDac();
            dac.DeleteList(ids);
            FunHelper.Alert("删除成功！", Request.Url.ToString());
        }

        #endregion

        #region 转向添加页面
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="eventArgs"></param>
        protected void lbAdd_Click(object obj, EventArgs eventArgs)
        {
            RedirectUrl("HotelsSet.aspx");
        }
        #endregion
    }
}